package com.eve.eplus.sns.weibo;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.eve.eplus.common.app.SettingConstants;
import com.eve.eplus.common.tools.ComTimeTools;
import com.eve.eplus.main.logic.SettingUtility;

/**
 * 微博返回Json处理工具
 * 
 * @author norsie
 * @create 13-01-10
 */
public class JsonTools
{

    /**
     * json预处理
     * 
     * @param jtext
     * @return
     */
    public static String parseJsonText(String jtext)
    {

        char leftSymbol = '[';
        char rightSymbol = ']';
        String result = "";
        for (int i = 0; i < jtext.length(); i++)
        {

            if (leftSymbol == jtext.charAt(i))
            {
                result = jtext.substring(i, jtext.length());
                break;
            }
        }

        for (int i = result.length(); i > 0; i--)
        {

            if (rightSymbol == result.charAt(i - 1))
            {
                result = result.substring(0, i);
                break;
            }
        }
        // System.out.println(result);
        return result;

    }
    
    /**
     * 通过UID获取用户的详细信息
     * 
     * @param rlt
     *            对应接口返回的json信息
     * @return
     */
    public static ArrayList<HashMap<String, String>> userShow(String rlt)
    {
        ArrayList<HashMap<String, String>> listArrays = new ArrayList<HashMap<String, String>>();
        try
        {
                JSONObject jsonObject = new JSONObject(rlt);
                HashMap<String, String> map = new HashMap<String, String>();

                String id = jsonObject.getString("id");// id
                String screen_name = jsonObject.getString("screen_name");// 用户昵称
                String gender = jsonObject.getString("gender");// 性别，m：男、f：女、n：未知
                String profile_image_url=jsonObject.getString("profile_image_url"); //头像地址
                
                map.put(UsersAPIDef.ID, id);
                map.put(UsersAPIDef.SCREEN_NAME, screen_name);
                map.put(UsersAPIDef.GENDER, gender);
                map.put(UsersAPIDef.PROFILE_IMAGE_URL, profile_image_url);

                listArrays.add(map);

        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }
        return listArrays;
    }

    /**
     * 获取当前登录用户及其所关注用户的最新微博
     * 
     * @param rlt
     *            对应接口返回的json信息
     * @return
     */
    public static ArrayList<HashMap<String, String>> friendsTimeline(String rlt)
    {
        ArrayList<HashMap<String, String>> listArrays = new ArrayList<HashMap<String, String>>();
        String result = parseJsonText(rlt);
        try
        {
            JSONArray jsonArrays = new JSONArray(result);
            for (int i = 0; i < jsonArrays.length(); i++)
            {
                JSONObject jsonObject = jsonArrays.getJSONObject(i);
                HashMap<String, String> map = new HashMap<String, String>();

                String created_at = jsonObject.getString("created_at");// 获得微博的发表时间
                String ID = jsonObject.getString("id");// 获得微博的ID
                String text = jsonObject.getString("text");// 获得微博内容
                String source = jsonObject.getString("source");// 获得微博来源
                String reposts_count = jsonObject.getString("reposts_count");// 获得转发数
                String comments_count = jsonObject.getString("comments_count");// 获得评论数

                // 生成user的可用JSON对象
                JSONObject userJson = jsonObject.getJSONObject("user");
                String name = userJson.getString("name");// 获得发表微博的用户名
                String profile_image_url = userJson.getString("profile_image_url");// 获得发表微博的头像
                String remark = userJson.getString("remark");// 获得备注名
                String verified = String.valueOf(userJson.getBoolean("verified")); // 获得是否认证

                map.put(StatusesAPIDef.ID, ID);
                map.put(StatusesAPIDef.TEXT, text);
                map.put(StatusesAPIDef.NAME, name);
                map.put(StatusesAPIDef.SOURCE, source);
                map.put(StatusesAPIDef.CREATED_AT,
                        timeFormat(created_at));
                map.put(StatusesAPIDef.REPOSTS_COUNT, reposts_count);
                map.put(StatusesAPIDef.COMMENTS_COUNT, comments_count);
                map.put(StatusesAPIDef.PROFILE_IMAGE_URL,
                        profile_image_url);
                map.put(StatusesAPIDef.REMARK, remark);
                map.put(StatusesAPIDef.VERIFIED, verified);

                // 读取设置
                String imgType;
                if (SettingUtility.getImgLoadTypeSetting(true) == SettingConstants.IMG_LOADTYPE_BMIDDLE)
                    imgType = "bmiddle_pic";
                else if (SettingUtility.getImgLoadTypeSetting(true) == SettingConstants.IMG_LOADTYPE_THUMBNAIL)
                    imgType = "thumbnail_pic";
                else
                    imgType = "thumbnail_pic";

                if (SettingUtility.getImgLoadTypeSetting(true) != SettingConstants.IMG_LOADTYPE_NONE)
                    if (jsonObject.has(imgType))
                    {
                        String thumbnail_pic = jsonObject.getString(imgType);// 获得缩略图片
                        map.put(StatusesAPIDef.PIC,
                                thumbnail_pic);
                    }
                    else if (jsonObject.has("thumbnail_pic"))
                    {
                        String thumbnail_pic = jsonObject.getString("thumbnail_pic");// 获得缩略图片
                        map.put(StatusesAPIDef.PIC,
                                thumbnail_pic);

                    }
                    else
                        map.put(StatusesAPIDef.PIC, null);
                else
                    map.put(StatusesAPIDef.PIC, null);
                
                //存储所有地址信息
                if (jsonObject.has("thumbnail_pic"))
                    map.put(StatusesAPIDef.PIC_THUMBNAIL,
                            jsonObject.getString("thumbnail_pic"));
                if (jsonObject.has("bmiddle_pic"))
                    map.put(StatusesAPIDef.PIC_BMIDDLE,
                            jsonObject.getString("bmiddle_pic"));
                if (jsonObject.has("original_pic"))
                    map.put(StatusesAPIDef.PIC_ORIGINAL,
                            jsonObject.getString("original_pic"));

                // 判断是否还有转发的内容
                if (jsonObject.has("retweeted_status"))
                {
                    JSONObject retweetedJson = jsonObject.getJSONObject("retweeted_status");
                    String retweetedText = retweetedJson.getString("text");// 获得转发微博的内容
                    JSONObject retWeetedUserNameJson = retweetedJson.getJSONObject("user");
                    String retweetedName = retWeetedUserNameJson.getString("name");// 获得转发微博内容的用户名
                    map.put(StatusesAPIDef.RETWEETED_TEXT, retweetedText);
                    map.put(StatusesAPIDef.RETWEETED_NAME, retweetedName);
                    if (SettingUtility.getImgLoadTypeSetting(true) != SettingConstants.IMG_LOADTYPE_NONE)
                        if (retweetedJson.has(imgType))
                        {
                            String retweetedthumbnail_pic = retweetedJson.getString(imgType);
                            map.put(StatusesAPIDef.RETWEETED_PIC,
                                    retweetedthumbnail_pic);
                        }
                        else if (retweetedJson.has("thumbnail_pic"))
                        {
                            String retweetedthumbnail_pic = retweetedJson.getString("thumbnail_pic");
                            map.put(StatusesAPIDef.RETWEETED_PIC,
                                    retweetedthumbnail_pic);
                        }
                        else
                        {
                            map.put(StatusesAPIDef.RETWEETED_PIC,
                                    null);
                        }
                    else
                        map.put(StatusesAPIDef.RETWEETED_PIC,
                                null);
                    
                    //存储所有地址信息
                    if (retweetedJson.has("thumbnail_pic"))
                        map.put(StatusesAPIDef.RETWEETED_PIC_THUMBNAIL,
                                retweetedJson.getString("thumbnail_pic"));
                    if (retweetedJson.has("bmiddle_pic"))
                        map.put(StatusesAPIDef.RETWEETED_PIC_BMIDDLE,
                                retweetedJson.getString("bmiddle_pic"));
                    if (retweetedJson.has("original_pic"))
                        map.put(StatusesAPIDef.RETWEETED_PIC_ORIGINAL,
                                retweetedJson.getString("original_pic"));
                }
                else
                {
                    map.put(StatusesAPIDef.RETWEETED_TEXT, null);
                    map.put(StatusesAPIDef.RETWEETED_NAME, null);
                }
                listArrays.add(map);
            }

        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }
        return listArrays;
    }

    // 处理时间表现形式
    private static String timeFormat(String GTMTime)
    {
        String now = ComTimeTools.getTime("yyyy-MM-dd HH:mm:ss");
        String time = ComTimeTools.getGMT2YYYY(GTMTime);
        if (now.substring(2, 10).equals(time.substring(2, 10)))
        {
            if (now.substring(11, 13).equals(time.substring(11, 13))
                    && Integer.parseInt(now.substring(14, 16))
                            - Integer.parseInt(time.substring(14, 16)) < 30)
            {
                if (now.substring(14, 16).equals(time.substring(14, 16)))
                    return Integer.parseInt(now.substring(17, 19))
                            - Integer.parseInt(time.substring(17, 19)) + "秒前";
                return (Integer.parseInt(now.substring(14, 16)) - Integer.parseInt(time.substring(
                        14, 16))) + "分钟前";
            }
            return time.substring(11, 16);
        }
        else
            return time.substring(6, 16);
    }

}
